package com.caigou.modules.home.ui

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.comm.base.widget.YearPickDialog
import com.caigou.modules.home.R
import com.caigou.modules.home.adapter.HistoryPrizeItemAdapter
import com.caigou.modules.home.bean.PrizeBean
import com.caigou.modules.home.databinding.ActivityHistoryPrizeBinding
import com.caigou.modules.home.viewmodel.HomeViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/12/19
 */
@Route(path = RouterPath.Home.PAGE_HISTORY_PRIZE)
@AndroidEntryPoint
class HistoryPrizeActivity : BaseActivity() {

    private lateinit var binding: ActivityHistoryPrizeBinding

    private val homeViewModel: HomeViewModel by viewModels()

    @JvmField
    @Autowired(name = AppConstants.PRIZE_TYPE)
    var prizeType: Int = 1

    private var year = Calendar.getInstance().get(Calendar.YEAR)

    private val mAdapter: HistoryPrizeItemAdapter by lazy {
        HistoryPrizeItemAdapter(this, mutableListOf())
    }

    private val yearDialog by lazy {
        YearPickDialog(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityHistoryPrizeBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.historyPrizeLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        binding.historyPrizeSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchHistoryPrize()
            }
        }

        binding.historyPrizeRecyclerView.run {
            layoutManager = LinearLayoutManager(this@HistoryPrizeActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@HistoryPrizeActivity))
        }

        binding.historyPrizeTvDate.text = year.toString()
        binding.historyPrizeTvDate.setOnClickListener {
            yearDialog.show()
        }
        yearDialog.tvConfirm.setOnClickListener {
            year = yearDialog.getYear()
            binding.historyPrizeTvDate.text = year.toString()
            fetchHistoryPrize()
            yearDialog.dismiss()
        }

        binding.historyPrizeTvBack.setOnClickListener {
            finish()
        }

        fetchHistoryPrize()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(homeViewModel.historyPrizeListLiveData, ::handleGetHistoryPrizeList)
    }

    private fun handleGetHistoryPrizeList(result: Resource<MutableList<PrizeBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.historyPrizeSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取历史开奖记录异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.historyPrizeSwipeRefreshLayout.isRefreshing = false
                if (result.data == null || result.data!!.isEmpty()) {
                    binding.historyPrizeLlEmptyComment.visibility = View.VISIBLE
                    binding.historyPrizeRecyclerView.visibility = View.GONE
                } else {
                    binding.historyPrizeLlEmptyComment.visibility = View.GONE
                    binding.historyPrizeRecyclerView.visibility = View.VISIBLE
                    mAdapter.setList(result.data!!)
                }
            }
        }
    }

    private fun fetchHistoryPrize() {
        homeViewModel.getHistoryPrizeList(prizeType, year)
    }
}